<?php

    //Sub Table Class define
    include("ClassSalesData.php");
    //Sub Table Class define
    include("ClassListingData.php");
    //Sub Table Class define
    include("ClassMLSInformation.php");
	//Sub Table Class define
    include_once("ClassSub.php");

class ListingClass
{	
	var $StreetAddress;				//SubClass
	var $ListingData;				//Data Class
	var $MLSInformation;			//MLS Class
	var $GeographicData;			//SubClass
	var $ModificationTimestamp;
	var $ListingID;
	var $Zoning;
	var $SalesData;					//Sales Class
	var $SchoolData;				//SubClass
	var $TaxID;					
	var $ParcelNumber;
	var $View;
	var $CopyrightNotice;
	var $Disclaimer;
	var $PictureData;				//SubClass
	
	var $ID;
	
	function AddItemFromXML($XmlArray)
	{
		$Array = $XmlArray->children();
			
		$this->StreetAddress = new StreetAddressClass;
		$this->StreetAddress->AddItemFromXML($Array[0]);
		$this->ListingData = new ListingDataClass;
		$this->ListingData->AddItemFromXML($Array[1]);
		$this->MLSInformation = new MLSInformationClass;
		$this->MLSInformation->AddItemFromXML($Array[2]);
		$this->GeographicData = new GeographicDataClass;
		$this->GeographicData->AddItemFromXML($Array[3]);
		
		$this->ModificationTimestamp = $Array[4];

		$this->ListingID = $Array[5];
		$this->Zoning = $Array[6];
	
		$this->SalesData = new SalesDataClass;
		$this->SalesData->AddItemFromXML($Array[7]);
		$this->SchoolData = new SchoolDataClass;
		$this->SchoolData->AddItemFromXML($Array[8]);
		
		$TaxData = $Array[9]->children();
		$this->TaxID = $TaxData[0];
		
		$this->ParcelNumber = $Array[10];
		$this->View = $Array[11];
		$this->CopyrightNotice = $Array[12];
		$this->Disclaimer = $Array[13];

		$this->PictureData = new PictureDataClass;
		$this->PictureData->AddItemFromXML($Array[1]);
	}
	
	function InsertSQL()
	{
		$strSqlCommand = "SELECT idListing FROM Listing ";
		$strSqlCommand .= " WHERE idListing = '".$this->ListingID."' ";
		$result = mysql_query($strSqlCommand);
		if( !$result )
		{
			echo $this->Listing->ListingID.$strSqlCommand." Search Failed<br/>";
		}
		if( @mysql_num_rows($result) == 0)
		{
		$this->StreetAddress->InsertSQL();
		$this->ListingData->InsertSQL();		
		$this->MLSInformation->InsertSQL();
		$this->GeographicData->InsertSQL();		
		$this->SalesData->InsertSQL();		
		$this->SchoolData->InsertSQL();
		$this->PictureData->InsertSQL();

		$strSqlCommand = "INSERT INTO Listing( idListing, StreetAddress_idStreetAddress, ";
		$strSqlCommand .="ListingData_idListingData, MLSInformation_idMLSInformation, ";
		$strSqlCommand .="GeographicData_idGeographicData, ModificationTimestamp, ";
		$strSqlCommand .="Zoning, SalesData_idSalesData, SchoolData_idSchoolData, ";
		$strSqlCommand .="TaxID, ParcelNumber, View, CopyrightNotice, Disclaimer, ";
		$strSqlCommand .="PictureData_idPictureData) ";
		$strSqlCommand .= "VALUES( '".$this->ListingID."', ";
		$strSqlCommand .= "'".$this->StreetAddress->ID."', ";
		$strSqlCommand .= "'".$this->ListingData->ID."', ";
		$strSqlCommand .= "'".$this->MLSInformation->ID."', ";
		$strSqlCommand .= "'".$this->GeographicData->ID."', ";
		$strSqlCommand .= "'".$this->ModificationTimestamp."', ";
		$strSqlCommand .= "'".$this->Zoning."', ";
		$strSqlCommand .= "'".$this->SalesData->ID."', ";
		$strSqlCommand .= "'".$this->SchoolData->ID."', ";
		$strSqlCommand .= "'".$this->TaxID."', ";
		$strSqlCommand .= "'".$this->ParcelNumber."', ";
		$strSqlCommand .= "'".$this->View."', ";
		$strSqlCommand .= "'".$this->CopyrightNotice."', ";
		$strSqlCommand .= "'".$this->Disclaimer."', ";
		$strSqlCommand .= "'".$this->PictureData->ID."') ";
				
		if(!mysql_query($strSqlCommand))
		{
			echo $strSqlCommand."<br/>";
		}
		}
	}
	
	function ModifySQL( )
	{
		$strSqlCommand = "SELECT * FROM Listing ";
		$strSqlCommand .= " WHERE idListing = '".$this->ListingID."' ";
		$result = mysql_query($strSqlCommand);

		$rowListing = @mysql_fetch_array($result);

		$strSqlCommand =  "UPDATE LotsAndLand SET ";
		$strSqlCommand .= "ModificationTimestamp='"	.$this->ModificationTimestamp."', ";
		$strSqlCommand .= "Zoning='"				.$this->Zoning			."', ";
		$strSqlCommand .= "TaxID='"					.$this->TaxID			."', ";
		$strSqlCommand .= "ParcelNumber='"			.$this->ParcelNumber	."', ";
		$strSqlCommand .= "View='"					.$this->View			."', ";
		$strSqlCommand .= "CopyrightNotice='"		.$this->CopyrightNotice	."', ";
		$strSqlCommand .= "Disclaimer='"			.$this->Disclaimer		."' ";
		$strSqlCommand .= "WHERE idListing = ".$this->ListingID;
		mysql_query($strSqlCommand);

		$this->StreetAddress->InsertSQL	( $rowListing["StreetAddress_idStreetAddress"] );
		$this->ListingData->InsertSQL	( $rowListing["ListingData_idListingData"]);		
		$this->MLSInformation->InsertSQL( $rowListing["MLSInformation_idMLSInformation"]);
		$this->GeographicData->InsertSQL( $rowListing["GeographicData_idGeographicData"]);		
		$this->SalesData->InsertSQL		( $rowListing["SalesData_idSalesData"]);		
		$this->SchoolData->InsertSQL	( $rowListing["SchoolData_idSchoolData"]);
		$this->PictureData->InsertSQL	( $rowListing["PictureData_idPictureData"]);		
	}
}
?>